package com.detrum.sportcamera.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.detrum.sportcamera.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BigImageFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BigImageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BigImageFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private byte[] bytes;
    private ImageView bigImage, close_iv;

    private OnFragmentInteractionListener mListener;

    public BigImageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment BigImageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BigImageFragment newInstance() {
        BigImageFragment fragment = new BigImageFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_big_image, container, false);
        bigImage = view.findViewById(R.id.bigImage);
        close_iv = view.findViewById(R.id.close_iv);
        close_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.close();
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mParam1!=null){
            Glide.with(getActivity()).load(mParam1).into(bigImage);
        }else if(bytes!=null){
            Glide.with(getActivity()).load(bytes).into(bigImage);
        }

    }

    public void setmParam1(String mParam1){
        this.mParam1 = mParam1;
    }

    public void setBytes(byte[]bytes){
        this.bytes = bytes;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
        void close();
    }
}
